<?php
/**
 * Created by PhpStorm.
 * User: cgp
 * Date: 14-8-22
 * Time: 上午9:38
 * 查找数据库中套票产品并生成静态数据
 */
define('ROOT_PATH','/var/www/html/new/');
define('FX_PATH', ROOT_PATH.'d/class/');

include FX_PATH . 'Log.class.php';
if (!$_GET['token'] && $_GET['token']!='cgp') {
    exit('Access Denied!');
}
if(!class_exists('go_sql')) {
    include ROOT_PATH.'conf/le.je';
    $le = new go_sql();
    $le->connect();
}
$lid = intval($_GET['lid']);
$p_type = $_GET['p_type'];
$package_id_list = array();
if ($p_type=='F') {
    $package_id_list[] = $lid;
    //是否需要用套票的attribute字段来获取景区名称
    $use_attribute = true;
}
else {
    //检验这个产品是否有被打包成套票
    $query = '{"lid":"'.$lid.'"}';
    $sql_chk = "SELECT id FROM uu_land WHERE instr(`attribute`, '\"lid\":\"$lid\"' )>0 AND p_type='F'";
    $le->query($sql_chk);
    while($tmp = $le->fetch_assoc()) {
        $package_id_list[] = $tmp['id'];
    }
    if ( !count($package_id_list )) {
        exit('没有被打包');
    }
    $getLandTitle = "SELECT id,title,imgpath,p_type FROM uu_land WHERE id=$lid AND status=1 ORDER BY id ASC ";
    $le->query($getLandTitle);
    $ProductTitles = array();
    while( $l = $le->fetch_assoc() ) {
        $ProductTitles[$l['id']] = array($l['title'], $l['imgpath']);
    }
}

//print_r($ProductTitles);
//TODO:获取套票的名称
$getPacketProducts = "SELECT id,attribute FROM uu_land WHERE id IN("
    .implode(',', $package_id_list).") AND p_type='F' AND status=1 ORDER BY id ASC";
$le->query($getPacketProducts);
$ProductLists = array();

$pidList = $lidList = array();
while( $row=$le->fetch_assoc() ) {
    $ProductLists[$row['id']] = json_decode($row['attribute'], true);
}
foreach($ProductLists as $pl) {
   foreach($pl as $p) {
       $pidList[] = $p['pid'];
       $lidList[] = $p['lid'];
   }
}

if (isset($use_attribute) && $use_attribute===true) {
    $getLandTitle = "SELECT id,title,imgpath,p_type FROM uu_land WHERE id IN("
        .implode(',', array_unique($lidList)).") AND status=1 ORDER BY id ASC";
    $le->query($getLandTitle);
    $ProductTitles = array();
    while( $l = $le->fetch_assoc() ) {
        $ProductTitles[$l['id']] = array($l['title'], $l['imgpath']);
    }
}

//TODO:获取套票的产品ID
$packagePid = array();
$getPackageId = "SELECT pid,landid FROM uu_jq_ticket WHERE landid IN("
    .implode(',', array_keys($ProductLists)).")";
$GLOBALS['le']->query($getPackageId);
while($row=$GLOBALS['le']->fetch_assoc()) {
    $packagePid[$row['landid']] = $row['pid'];
}
//TODO:计算完整的数据
$IntersectKey = array_keys( array_intersect_key($packagePid, $ProductLists) );

//TODO:获取套票中每个票类的名称
$sqlTicketTitle = "SELECT title,pid FROM uu_jq_ticket WHERE pid IN("
    .implode(',', $pidList).")";
//echo $sqlTicketTitle;
$le->query($sqlTicketTitle);
$ticketTitles = array();
while($t=$le->fetch_assoc()) {
    $ticketTitles[$t['pid']] = $t['title'];
}

//var_dump($IntersectKey);
foreach ($ProductLists as $lid=>$pros) {
    $Links = array();
    if (!in_array($lid, $IntersectKey)) {
        continue;
    }
    $len = count($pros);
    for($i=0; $i<$len; $i++) {
        $linkIds = array();
        $lPid  = $pros[$i]['pid'];
//        $key = $lid.'_'.$lPid;
        $lidIn = $pros[$i]['lid'];
        $Links['lid']        = $lid;//套票景区ID
        $Links['pid']        = $packagePid[$lid];//套票PID
        $Links['l_title']    = $ProductTitles[$lidIn][0];
        $Links['l_img']      = $ProductTitles[$lidIn][1];
        $Links['t_title']    = $ticketTitles[$lPid];
//        $Links[$lPid]['aid']        = $ProductTitles[$i]['aid'];//供应商ID
        $Links['num']        = $pros[$i]['num'];
//        $Links[$lPid]['lid'] = $lid;
        foreach($pros as $pro) {
            if ($pro['pid']==$lPid) {
                continue;
            }
            $linkIds[$pro['pid']] = array(
                'l_title' => $ProductTitles[$pro['lid']][0],
                'l_img'   => $ProductTitles[$pro['lid']][1],
                't_title' => $ticketTitles[$pro['pid']],
                'num'     => $pro['num']
            );
        }
        $Links['link_ids'] = $linkIds;
        //SAVE TO FILES
        $saveStr = "<?php\nreturn " . var_export($Links, true)."\n?>";
        $fileName = dirname(__FILE__) . "/data/p_{$lPid}_{$lid}.inc.php";
        if (!$handle = fopen($fileName, 'w')) {
            \pft\Log\Log::writeLog( date('Y-m-d H:i:s') . "|不能打开文件 $fileName", 'log.log');
            exit;
        }
        if (fwrite($handle, $saveStr) === FALSE) {
            \pft\Log\Log::writeLog( date('Y-m-d H:i:s') . "|不能写入到文件 $fileName", 'log.log');
            exit;
        }
        //\pft\Log\Log::writeLog( date('Y-m-d H:i:s') ."|关联产品数据生成成功", 'log.log');
    }
    //print_r($Links);exit;

}
